package com.ikangtai.shecare.curve;

import android.content.res.Configuration;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;

import com.ikangtai.shecare.R;
import com.ikangtai.shecare.common.baseView.TopBar;
import com.jaeger.library.StatusBarUtil;

import me.imid.swipebacklayout.lib.SwipeBackLayout;
import me.imid.swipebacklayout.lib.app.SwipeBackActivity;

public class MucusChartActivity extends SwipeBackActivity {

    private WebView webview;
    private TopBar topBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mucuschart_layout);

        SwipeBackLayout mSwipeBackLayout = getSwipeBackLayout();
        //设置滑动方向，可设置EDGE_LEFT, EDGE_RIGHT, EDGE_ALL, EDGE_BOTTOM
        mSwipeBackLayout.setEdgeTrackingEnabled(SwipeBackLayout.EDGE_LEFT);
        StatusBarUtil.setColor(this, getResources().getColor(R.color.mucus_color),0);

        initView();

    }

    private final void initView(){
        webview = (WebView) findViewById(R.id.webview);

        WebViewCfg webViewCfg = new WebViewCfg(webview);
        webview = webViewCfg.getCfgedWebView(this);
        webview.loadUrl("file:///android_asset/health/mucusChart.html");

        topBar = (TopBar) findViewById(R.id.topBar);
        topBar.setOnTopBarClickListener(new TopBar.OnTopBarClickListener() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void midLeftClick() {
            }

            @Override
            public void midRightClick() {
            }

            @Override
            public void rightClick() {
            }
        });
    }

    private class ImgViewBackClickListener implements View.OnClickListener{
        @Override
        public void onClick(View view){
            finish();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK) { //监控/拦截/屏蔽返回键
            MucusChartActivity.this.finish();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    //必须捕捉，否则Activity onDestroy然后onCreate，js不能有效的监听到横竖屏的变化;
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        // 当新设置中，屏幕布局模式为横排时
        if(this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
            //TODO 横屏，调整webview为全屏
        }
        else if(this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
            //TODO 竖屏，调整webview和下半部分的比例
        }
        super.onConfigurationChanged(newConfig);
    }

    //防止内存泄露
    @Override
    protected void onDestroy(){
        webview.destroy();
        webview.removeAllViews();
        super.onDestroy();
    }
}
